package com.ytc.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ytc.commons.constants.CommonConstants;
import com.ytc.commons.model.dto.CommentReplyDto;
import com.ytc.commons.model.vo.CommentReplyVo;
import com.ytc.commons.model.vo.ResponseResult;
import com.ytc.commons.model.vo.User;
import com.ytc.commons.utils.AuthInfoHolder;
import com.ytc.commons.utils.BeanHelper;
import com.ytc.system.api.SystemClient;
import com.ytc.teaching.domain.Comment;
import com.ytc.teaching.domain.CommentReply;
import com.ytc.teaching.mappers.CommentReplyMapper;
import com.ytc.teaching.service.CommentReplyService;
import com.ytc.teaching.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-10-10
 */
@Service
public class CommentReplyServiceImpl extends ServiceImpl<CommentReplyMapper, CommentReply> implements CommentReplyService {
    @Autowired
    private CommentService commentService;
    @Autowired
    private SystemClient systemClient;

    /**
     * 教育机构---课程评论回复
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult courseCommentReply(CommentReplyDto dto) {

        CommentReply commentReply = new CommentReply();
        //根据id查询出评论表数据 设置回复状态
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        qw.eq(Comment::getCommentId, dto.getCommentId());
        Comment comment = commentService.getOne(qw);
        comment.setReplyStatus(CommonConstants.USING_FLAG);
        //保存数据
        commentService.updateById(comment);
        commentReply.setUserId(AuthInfoHolder.getUserId());
        commentReply.setStatus(CommonConstants.USING_FLAG);
        commentReply.setReplyText(dto.getReplyText());
        commentReply.setReplyDate(new Date());
        commentReply.setParentId(0L);
        commentReply.setNickName(comment.getNickName());
        commentReply.setCompanyId(AuthInfoHolder.getCompanyId());
        ResponseResult<User> result = systemClient.findById(AuthInfoHolder.getUserId().longValue());
        User user = result.getData();
        commentReply.setUserName(user.getName());
        commentReply.setPraiseNum(0);
        commentReply.setCommentId(dto.getCommentId().longValue());
        this.save(commentReply);
        CommentReplyVo vo = BeanHelper.copyProperties(commentReply, CommentReplyVo.class);
        return ResponseResult.okResult(vo);
    }
}
